package com.pay.service.impl;

import com.pay.constant.Constant;
import com.pay.entity.Merchant;
import com.pay.entity.Order;
import com.pay.mapper.OrderMapper;
import com.pay.resource.SpringBeanResource;
import com.pay.service.IMerchantService;
import com.pay.service.IOrderService;
import com.pay.utils.BasePage;
import com.pay.utils.MD5Util;
import com.pay.utils.ResponseData;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Peng
 * @since 2018-12-07
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

	@Autowired
	private IMerchantService merchantService ;
	
	@Override
	public Order getByPlatno(String platno) {
		return super.getOne(new QueryWrapper<Order>().eq("platno", platno)) ;
	}

	@Override
	public void getList(Order order, BasePage<Order> page) {
		QueryWrapper<Order> ew = new QueryWrapper<Order>();
		ew.orderByDesc("id") ;
		if(order.getMerchantId() != null) {
			ew.eq("merchant_id", order.getMerchantId()) ;
		}
		if(page.isPage()) {
			super.page(page, ew) ;
		}else {
			List<Order> data = super.list(ew);
			int total = super.count(ew);
			page.setRecords(data) ;
			page.setTotal(total) ;
		}
		
		for(Order o:page.getRecords()) {
			Merchant merchant = merchantService.getById(o.getMerchantId());
			o.setMerchant(merchant); 
		}
		
	}

	@Override
	public ResponseData doOrderNotify(Integer id) {
		Order order = super.getById(id);
		if(order.getNotifystatus() == Constant.notify_success) {
			return ResponseData.baseError("已经回调成功") ;
		}
		
		/**
		 * String str= orderno + paystatus+ amount+ platno+ successtime; 
		String sign= MD5(MD5(str) + key);
		 */
		JSONObject param = new JSONObject();
		param.put("orderno", order.getOrderno()) ; //商户订单号
		param.put("platno", order.getPlatno()) ; //平台订单号
		param.put("paystatus", order.getPaystatus()) ; //除了1其他都是失败
		param.put("amount", order.getAmount()) ; //金额单位分
		long successtime = order.getSuccesstime() == null ? 0L : order.getSuccesstime().getTime() ;
		param.put("successtime", successtime) ; //支付时间
		Merchant merchant = SpringBeanResource.merchantService.getById(order.getMerchantId());
		
		String str = order.getOrderno() + order.getPaystatus() + order.getAmount() + order.getPlatno() + successtime ;
		String sign = MD5Util.md5Hex(MD5Util.md5Hex(str) + merchant.getMerchantKey());
		param.put("sign", sign) ; //和提交订单一致的签名
		
		order.setNotifycount(order.getNotifycount() + 1);
		super.updateById(order) ;
		
		String result = "";
		try {
			result = HttpUtil.post(order.getNotify(), param.toString());
		} catch (Exception e) {
			result = e.getMessage() ;
		}
		if("success".equalsIgnoreCase(result)) {
			//成功
			order.setNotifystatus(Constant.notify_success);  //回调成功
			super.updateById(order) ; 
			return ResponseData.ok() ;
		}
		return ResponseData.baseError(result) ;
	}

}
